<!DOCTYPE html>
<html lang="zh-Hans">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="突然想测试下在高并发的情况下自己代码的运行情况,于是开始试着使用 http_load"/>




  <meta name="keywords" content="生活, 工作, 杂鱼君的博客" />










  <link rel="alternate" href="/atom.xml" title="杂鱼君的博客">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="http://notes.wen.moe/post/linux-pressure-testing-tool.html"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  



  <script id="baidu_push">
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>









    <title> 使用 http_load 测试服务器的负载 - 杂鱼君的博客 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">杂鱼君的博客</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/">
        <li class="mobile-menu-item">
          
          
            Home
          
        </li>
      </a>
    
      <a href="/archives/">
        <li class="mobile-menu-item">
          
          
            Archives
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            Tags
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            About
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">杂鱼君的博客</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/">
            
            
              Home
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives/">
            
            
              Archives
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              Tags
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              About
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          使用 http_load 测试服务器的负载
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2016-07-14
        </span>
        
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">Contents</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#安装"><span class="toc-text">安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用"><span class="toc-text">使用</span></a></li></ol>
    </div>
  </div>


    <div class="post-content">
      
        <p>http_load是一款linux,OSX下的性能检测工具,通过简单的参数就能测试Web服务器的负载和性能.对于日常的代码自测已经足够了.</p>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p>这个很简单,OSX下只需要:</p>
<figure class="highlight mipsasm"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">brew </span><span class="keyword">install </span>http_load</div></pre></td></tr></table></figure>
<p>通过 brew 就可以直接装了.</p>
<p>ubuntu 下需要下载源码自己编译:</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">wget http:<span class="comment">//acme.com/software/http_load/http_load-09Mar2016.tar.gz</span></div><div class="line"></div><div class="line">tar zxvf http_load-<span class="number">09</span>Mar2016<span class="selector-class">.tar</span><span class="selector-class">.gz</span></div><div class="line"></div><div class="line">cd http_load-<span class="number">09</span>Mar2016</div><div class="line"></div><div class="line">make &amp;&amp; make install</div></pre></td></tr></table></figure>
<p>前提是需要安装 <code>wget</code> 和 <code>gcc</code>.</p>
<p>编译完成以后就可以再当前文件夹下看到 http_load 文件了,为了方便我们可以:</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo cp http_load <span class="regexp">/usr/</span>local<span class="regexp">/bin/</span>http_load</div></pre></td></tr></table></figure>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><p>基本的使用方式如下:</p>
<figure class="highlight accesslog"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">http_load <span class="string">[参数]</span> <span class="string">[url_file]</span></div></pre></td></tr></table></figure>
<p><strong>url_file文件内容如下</strong></p>
<figure class="highlight dts"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="symbol">http:</span><span class="comment">//a.com/test1</span></div><div class="line"><span class="symbol">http:</span><span class="comment">//a.com/test2</span></div><div class="line"><span class="symbol">http:</span><span class="comment">//a.com/test3</span></div><div class="line"><span class="symbol">http:</span><span class="comment">//a.com/test4</span></div></pre></td></tr></table></figure>
<p>里面包含你需要访问的url,用回车分割.域名,IP都可以.</p>
<p><strong>参数含义如下</strong></p>
<p>-p (parallels) : 并发的访问进程数<br>-f (fetches) : 总的访问数<br>-r (rates) : 每秒的请求数<br>-s (seconds) : 总的请求时间</p>
<p>通过这四个参数组合,我们就很容易针对性的做一些测试了,比如我们想测试服务器的QPS:</p>
<figure class="highlight lsl"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">http_load -r <span class="number">200</span> -s <span class="number">10</span> url_file</div></pre></td></tr></table></figure>
<p>意味着每秒请求200次,总共请求10秒.</p>
<p>或者:</p>
<figure class="highlight lsl"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">http_load -p <span class="number">100</span> -f <span class="number">10000</span> url_file</div></pre></td></tr></table></figure>
<p>返回的结果一般如下:</p>
<figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="number">1301</span> fetches, <span class="number">170</span> <span class="built_in">max</span> parallel, <span class="number">383256</span> <span class="keyword">bytes</span>, <span class="keyword">in</span> <span class="number">10.0006</span> <span class="built_in">seconds</span></div><div class="line"><span class="number">294.586</span> mean <span class="keyword">bytes</span>/connection</div><div class="line"><span class="number">130.093</span> fetches/<span class="built_in">sec</span>, <span class="number">38323.5</span> <span class="keyword">bytes</span>/<span class="built_in">sec</span></div><div class="line">msecs/connect: <span class="number">0.488161</span> mean, <span class="number">5.405</span> <span class="built_in">max</span>, <span class="number">0.138</span> <span class="built_in">min</span></div><div class="line">msecs/<span class="keyword">first</span>-response: <span class="number">653.768</span> mean, <span class="number">1645.57</span> <span class="built_in">max</span>, <span class="number">0.174</span> <span class="built_in">min</span></div><div class="line"><span class="number">176</span> bad <span class="keyword">byte</span> counts</div><div class="line">HTTP response codes:</div><div class="line">  code <span class="number">200</span> <span class="comment">-- 1125</span></div><div class="line">  code <span class="number">500</span> <span class="comment">-- 168</span></div></pre></td></tr></table></figure>
<p>其中<code>msecs/connect</code> <code>msecs/first-response</code> 的三个数值告诉我们平均响应时间,最大响应时间,最小响应时间(单位是毫秒)<br>具体区别不太明白,待补充.</p>
<p>最后三行是统计的HTTP状态码,可以大致了解响应的情况.</p>

      
    </div>

    
      
      

  <div class="post-copyright">
    <p class="copyright-item">
      <span>Author: </span>
      <a href="http://notes.wen.moe">zakokun</a>
    </p>
    <p class="copyright-item">
      <span>Link: </span>
      <a href="http://notes.wen.moe/post/linux-pressure-testing-tool.html">http://notes.wen.moe/post/linux-pressure-testing-tool.html</a>
    </p>
    <p class="copyright-item">
      <span>License: </span>
      
      <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/生活/">生活</a>
            
              <a href="/tags/工作/">工作</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/post/golang-time-package.html">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">phper 学 golang --日期时间处理</span>
        <span class="prev-text nav-mobile">Prev</span>
      </a>
    
    
      <a class="next" href="/post/impression-about-phpcon-2016.html">
        <span class="next-text nav-default">PHPCON2016 的一些心得</span>
        <span class="prev-text nav-mobile">Next</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:me@wen.moe" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
        
          <a href="https://www.instagram.com/zakokun" class="iconfont icon-instagram" title="instagram"></a>
        
      
    
    
    
  </div>


<div class="copyright">
  <span class="power-by">
    Powered by <a class="hexo-link" href="https://hexo.io/">Hexo</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Theme -
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Hosted by <a class="theme-link" href="https://pages.coding.me" style="font-weight: bold">Coding Pages</a>
  </span>

  <span class="copyright-year">
    
    &copy;
    
      2014 -
    
    2017

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">zakokun</span>
  </span>
</div>

      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'http://notes.wen.moe/post/linux-pressure-testing-tool.html';
        this.page.identifier = 'post/linux-pressure-testing-tool.html';
        this.page.title = '使用 http_load 测试服务器的负载';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//evanszhang.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>




    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>

  </body>
</html>
